<?php
namespace Model;
/**
 * model obsługujący ogłoszenia
 */
class Ad extends \Model {
	/**
	 * konstruktor
	 * @param mixed $number parametr określający co pobrać
	 */
	public function __construct ($data) {
		self::init();
		if($data['ad'] == 'all') {
			$this->getAll($data['category']);
		} else {
			$this->getSingle($data['category'], $data['ad']);
		}
	}
	/**
	 * pobiera wszystkie ogłoszenia z bazy
	 */
	private function getAll ($category) {
		$log = new \Log('Model\Ad::getAll');
		$log->add('<pre>'.print_r($category, 1).'</pre>');
		$collection  = self::$database->posts;
		$posts = $collection->find(array('path' => implode('/', $category)), array('title' => 1, 'description' => '1'));
		foreach ($posts as $post) {
			$this->data[] = array(
				'title' => $post['title'],
				'description' => $post['description']
			);
		}
		$this->data['mode'] = 'category';
	}
	/**
	 * pobiera ogłoszenie o określonym numerze
	 * @param int $number numer ogłoszenia
	 */
	private function getSingle ($category, $ad) {
		$log = new \Log('AdModel::getSingle');
		$log->add('<pre>'.print_r($category, true).'</pre>');
		$log->add($ad);
		$collection = self::$database->posts;
		$post = $collection->findOne(array('alias' => $ad, 'path' => implode('/', $category)));
		if(is_array($post)) {
			$this->data['title'] = $post['title'];
			$this->data['description'] = $post['description'];
			$this->data['fullDescription'] = $post['fullDescription'];
			$this->data['mode'] = 'single';
		} else {
			$log->add('wałek - ogłoszenie nie istnieje');
			$log->add('post: '.print_r($post, true));
		}
	}
}
?>